package org.qloudgen.borg.pages;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.wicket.extensions.markup.html.tabs.AbstractTab;
import org.apache.wicket.extensions.markup.html.tabs.TabbedPanel;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.Model;
import org.apache.wicket.request.mapper.parameter.PageParameters;
import org.qloudgen.borg.CommonValues;
import org.qloudgen.borg.beans.FuncBean;
import org.qloudgen.borg.metadb.DefaultImpl;
import org.qloudgen.borg.pages.common.BasePage;
import org.qloudgen.borg.pages.compnent.CancelButton;
import org.qloudgen.borg.pages.panel.AppFuncPanel;
import org.qloudgen.borg.pages.panel.AppFuncParameterPanel;
import org.qloudgen.borg.pages.panel.AppFuncResultPanel;

import com.google.common.collect.Lists;

public class AppFuncCreatePage extends BasePage {
	
	/**
	 * @Author youngeryoyo
	 * youngeryoyo@gmail.com
	 */
	static Log log = LogFactory.getLog(AppFuncCreatePage.class);
	
	private static final long serialVersionUID = 1L;
	private transient FuncBean  func = new FuncBean();
	
	public AppFuncCreatePage(PageParameters pageParameters){
		super();
		
		this.pp = pageParameters;
		
		if (this.pp.get("appName") != null) {
			func.setAppNo(this.pp.get("appNo").toString());
			func.setAppName(this.pp.get("appName").toString());
		}

	    final List<AbstractTab> tabs=Lists.newArrayList();
		
		// form
		final Form<Object> form = new Form<Object>("form") {
			
			private static final long serialVersionUID = 1L;
			protected void onSubmit() {
				
				if (findSubmittingButton() !=  null && isCreate()) {
				
     				DefaultImpl.createAppFunc(func);
					setResponsePage(AppFuncListPage.class,pp);

				}
			}
			
		};
		this.add(form);
		
		// tabs for func info 
	    tabs.add(new AbstractTab(new Model<String>("基本信息")) {
			private static final long serialVersionUID = 1L;
			public Panel getPanel(String panelId) { 
				return new AppFuncPanel(panelId,func);
			}
	    });
	    
	    tabs.add(new AbstractTab(new Model<String>("功能参数")) {
			private static final long serialVersionUID = 1L;
			public Panel getPanel(String panelId) { 
				return new AppFuncParameterPanel(panelId,func);
			}
	    });
	    
	    tabs.add(new AbstractTab(new Model<String>("结果表列")) {
			private static final long serialVersionUID = 1L;
			public Panel getPanel(String panelId) { 
				return new AppFuncResultPanel(panelId,func);
			}
	    });
	    
	    form.add(new TabbedPanel<AbstractTab>("tabs", tabs));
	    
	    // submit or cancel 
		form.add( new Button ("submit"));
		form.add(new CancelButton("cancel",AppFuncListPage.class,pp));
		
	}

	private boolean isCreate(){
		
		boolean flag = true;
		if (func.getTableFile() == null || func.getHandlerFile() == null) {
			info(CommonValues.INFO_NOT_NULL);
			flag = false;
		} else if (func.getParams().size() == 0) {
			info(CommonValues.INFO_FUNC_NOT_PARAMS);
			flag = false;
		} else if (func.getColumns().size() == 0) {
			info(CommonValues.INFO_FUNC_NOT_RESULT);
			flag = false;
		} 
		
		return flag;
	}
}
